﻿#include "config.h"

#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QJsonValueRef>
#include <QFile>
#include <QByteArray>
#include <QDataStream>
#include <QtDebug>
#include<QTextCodec>
// ConfigRouterItem key
const QString ConfigRouterItem::keyIp(QObject::tr("ip"));
const QString ConfigRouterItem::keyPort(QObject::tr("port"));
const QString ConfigRouterItem::keyAddrs(QObject::tr("addrs"));
// ConfigRouter
const QString ConfigRouter::key(QObject::tr("router"));

// ConfigGyscada key
const QString ConfigGyscada::keyIp(QObject::tr("ip"));
const QString ConfigGyscada::keyPort(QObject::tr("port"));
const QString ConfigGyscada::keyT0(QObject::tr("t0"));
const QString ConfigGyscada::keyT1(QObject::tr("t1"));
const QString ConfigGyscada::keyT2(QObject::tr("t2"));
const QString ConfigGyscada::keyTimer(QObject::tr("timer"));
const QString ConfigGyscada::keyGyscada(QObject::tr("gyscada"));

// ConfigMySQL key
const QString ConfigMySQL::keyIp(QObject::tr("ip"));
const QString ConfigMySQL::keyPort(QObject::tr("port"));
const QString ConfigMySQL::keyDB(QObject::tr("db"));
const QString ConfigMySQL::keyUser(QObject::tr("user"));
const QString ConfigMySQL::keyPassword(QObject::tr("password"));
const QString ConfigMySQL::keyUserTable(QObject::tr("user_table"));
const QString ConfigMySQL::keyStafferTable(QObject::tr("staffer_table"));
const QString ConfigMySQL::keyLogTable(QObject::tr("log_table"));
const QString ConfigMySQL::key(QObject::tr("mysql"));

// ConfigTransitionItem key
const QString ConfigTransitionItem::keyId(QObject::tr("id"));
const QString ConfigTransitionItem::keyDefaultState(QObject::tr("defaultstate"));
const QString ConfigTransitionItem::keyTemplates(QObject::tr("templates"));
const QString ConfigTransitionItem::keyTemplatesPoint(QObject::tr("point"));
const QString ConfigTransitionItem::keyTemplatesState(QObject::tr("state"));
// ConfigTransition key
const QString ConfigTransition::key(QObject::tr("transition"));

// ConfigPointItem key
const QString ConfigPointItem::keyId(QObject::tr("id"));
const QString ConfigPointItem::keyTitle(QObject::tr("title"));
const QString ConfigPointItem::keyUnit(QObject::tr("unit"));
// ConfigPoint key
const QString ConfigPoint::key(QObject::tr("point"));

// ConfigStationItem key
const QString ConfigStationItem::keyId(QObject::tr("id"));
const QString ConfigStationItem::keyName(QObject::tr("name"));
const QString ConfigStationItem::keyLines(QObject::tr("lines"));
const QString ConfigStationItem::keyControls(QObject::tr("controls"));
// ConfigStation key
const QString ConfigStation::key(QObject::tr("station"));

// ConfigLineItem key
const QString ConfigLineItem::keyId(QObject::tr("id"));
const QString ConfigLineItem::keyName(QObject::tr("name"));
const QString ConfigLineItem::keyTurbines(QObject::tr("turbines"));
const QString ConfigLineItem::keyControls(QObject::tr("controls"));
// ConfigLine key
const QString ConfigLine::key(QObject::tr("line"));

// ConfigTurbineItem key
const QString ConfigTurbineItem::keyId(QObject::tr("id"));
const QString ConfigTurbineItem::keyName(QObject::tr("name"));
const QString ConfigTurbineItem::keyControls(QObject::tr("controls"));
const QString ConfigTurbineItem::keyStatePoint(QObject::tr("statepoint"));
const QString ConfigTurbineItem::keyStateTransition(QObject::tr("statetransition"));
const QString ConfigTurbineItem::keyPoints(QObject::tr("points"));
const QString ConfigTurbineItem::keyPointsId(QObject::tr("id"));
const QString ConfigTurbineItem::keyPointsAddr(QObject::tr("addr"));
// ConfigTurbine key
const QString ConfigTurbine::key(QObject::tr("turbine"));

// ConfigControlItem key
const QString ConfigControlItem::keyId(QObject::tr("id"));
const QString ConfigControlItem::keyTitle(QObject::tr("title"));
const QString ConfigControlItem::keyDescription(QObject::tr("description"));
const QString ConfigControlItem::keyShortCut(QObject::tr("shortcut"));
const QString ConfigControlItem::keyIcon(QObject::tr("icon"));
const QString ConfigControlItem::keyType(QObject::tr("type"));
const QString ConfigControlItem::keyDlgTitle(QObject::tr("dlgtitle"));
const QString ConfigControlItem::keyDefaultValue(QObject::tr("defaultvalue"));
const QString ConfigControlItem::keyMinValue(QObject::tr("minvalue"));
const QString ConfigControlItem::keyMaxValue(QObject::tr("maxvalue"));
const QString ConfigControlItem::keyDecimals(QObject::tr("decimals"));
// ConfigControl key
const QString ConfigControl::key(QObject::tr("control"));

// ConfigStateItem key
const QString ConfigStateItem::keyId(QObject::tr("id"));
const QString ConfigStateItem::keyTitle(QObject::tr("title"));
const QString ConfigStateItem::keyDescription(QObject::tr("description"));
const QString ConfigStateItem::keyColor(QObject::tr("color"));
const QString ConfigStateItem::keyIcon(QObject::tr("icon"));
// ConfigState key
const QString ConfigState::key(QObject::tr("state"));

// GYSCADAConfig instance;
GYSCADAConfig* GYSCADAConfig::instance = NULL;
const QString GYSCADAConfig::fileName(QObject::tr("config/gyscada.json"));

GYSCADAConfig::GYSCADAConfig()
{
}

GYSCADAConfig* GYSCADAConfig::getInstance()
{
    if (GYSCADAConfig::instance == NULL) {
        GYSCADAConfig::instance = new GYSCADAConfig;
    }

    return GYSCADAConfig::instance;
}

// Parse config file
bool GYSCADAConfig::parse()
{
    // Open Config file
    QFile file(GYSCADAConfig::fileName);
    if (!file.open(QIODevice::ReadOnly)) {
        return false;
    }

    if (file.size() <= 0) {
        file.close();
        return false;
    }

    char* buffer = new char[file.size()];
    int length = file.size();

    QDataStream stream(&file);
    length = stream.readRawData(buffer, length);
    file.close();
    if (length <= 0) {
        return false;
    }

    // Parse Config file
    QByteArray byteArray(buffer, length);
    QJsonParseError parseError;
    QJsonDocument document = QJsonDocument::fromJson(byteArray, &parseError);
    if (parseError.error != QJsonParseError::NoError) {
        return false;
    }

    if (!document.isObject()) {
        return false;
    }

    QJsonObject objRoot = document.object();
    // Parse gyscada
    if (!parseGyscada(objRoot)) {
        return false;
    }

    // Parse mysql
    if (!parseMySQL(objRoot)) {
        return false;
    }

    // Parse control category
    if (!parseControl(objRoot)) {
        return false;
    }

    // Parse State category
    if (!parseState(objRoot)) {
        return false;
    }

    // Parse Turbine
    if (!parseTurbine(objRoot)) {
        return false;
    }

    // Parse Line
    if (!parseLine(objRoot)) {
        return false;
    }

    // Parse Station
    if (!parseStation(objRoot)) {
        return false;
    }

    // Parse Point
    if (!parsePoint(objRoot)) {
        return false;
    }

    // Parse Transition
    if (!parseTransition(objRoot)) {
        return false;
    }

    // Parse Router
    if (!parseRouter(objRoot)) {
        return false;
    }

    // Fill control type
    // fillControlInfo();

    return true;
}

void GYSCADAConfig::fillControlInfo()
{
    QList<int> keyControl = GYSCADAConfig::getInstance()->control.controls.keys();
    QList<int> keyTurbine = GYSCADAConfig::getInstance()->turbine.turbines.keys();

    for (QList<int>::iterator itControl = keyControl.begin();
         itControl != keyControl.end();
         ++itControl) {
        for (QList<int>::iterator itTurbine = keyTurbine.begin();
             itTurbine != keyTurbine.end();
             ++itTurbine) {
            if (GYSCADAConfig::getInstance()->turbine.turbines[*itTurbine].controls.contains(*itControl)) {
                GYSCADAConfig::getInstance()->turbine.turbines[*itTurbine].controls[*itControl].type =
                GYSCADAConfig::getInstance()->control.controls[*itControl].type;
            }
        }
    }
}

bool GYSCADAConfig::parseRouter(QJsonObject &objRoot)
{
    // Parse Router
    if (!objRoot.contains(ConfigRouter::key)) {
        return false;
    }
    QJsonValue valueRouter = objRoot.take(ConfigRouter::key);
    if (!valueRouter.isArray()) {
        return false;
    }
    QJsonArray arrayRouter = valueRouter.toArray();
    for (int i = 0; i < arrayRouter.size(); ++i) {
        ConfigRouterItem configRouterItem;

        // Parse router item
        QJsonValue valueRouterItem = arrayRouter.at(i);
        if (!valueRouterItem.isObject()) {
            return false;
        }
        QJsonObject objRouterItem = valueRouterItem.toObject();
        if (!objRouterItem.contains(ConfigRouterItem::keyIp) ||
            !objRouterItem.contains(ConfigRouterItem::keyPort) ||
            !objRouterItem.contains(ConfigRouterItem::keyAddrs)) {
            return false;
        }

        // Parse ip
        QJsonValue valueRouterItemIp = objRouterItem.take(ConfigRouterItem::keyIp);
        if (!valueRouterItemIp.isString()) {
            return false;
        }
        configRouterItem.ip = valueRouterItemIp.toString();

        // Parse port
        QJsonValue valueRouterItemPort = objRouterItem.take(ConfigRouterItem::keyPort);
        if (!valueRouterItemPort.isDouble()) {
            return false;
        }
        configRouterItem.port = valueRouterItemPort.toInt();

        // Parse addrs
        QJsonValue valueRouterItemAddrs = objRouterItem.take(ConfigRouterItem::keyAddrs);
        if (!valueRouterItemAddrs.isArray()) {
            return false;
        }
        QJsonArray arrayRouterItemAddrs = valueRouterItemAddrs.toArray();
        for (int i = 0; i < arrayRouterItemAddrs.size(); ++i) {
            configRouterItem.addrs.append(arrayRouterItemAddrs.at(i).toInt());
        }

        // Insert into ConfigState
        GYSCADAConfig::getInstance()->router.routers.append(configRouterItem);
    }

    return true;
}

bool GYSCADAConfig::parseTransition(QJsonObject &objRoot)
{
    // Parse State Transition
    if (!objRoot.contains(ConfigTransition::key)) {
        return false;
    }
    QJsonValue valueTransition = objRoot.take(ConfigTransition::key);
    if (!valueTransition.isArray()) {
        return false;
    }

    QJsonArray arrayTransition = valueTransition.toArray();
    for (int i = 0; i < arrayTransition.size(); ++i) {
        int key = 0;
        ConfigTransitionItem configTransitionItem;

        // Parse Transition item
        QJsonValue valueTransitionItem = arrayTransition.at(i);
        if (!valueTransitionItem.isObject()) {
            return false;
        }
        QJsonObject objTransitionItem = valueTransitionItem.toObject();
        if (!objTransitionItem.contains(ConfigTransitionItem::keyId) ||
            !objTransitionItem.contains(ConfigTransitionItem::keyDefaultState) ||
            !objTransitionItem.contains(ConfigTransitionItem::keyTemplates)) {
            return false;
        }

        // Parse id
        QJsonValue valueTransitionItemId = objTransitionItem.take(ConfigTransitionItem::keyId);
        if (!valueTransitionItemId.isDouble()) {
            return false;
        }
        key = configTransitionItem.id = valueTransitionItemId.toInt();

        // Parse defaultstate
        QJsonValue valueTransitionItemDefaultState = objTransitionItem.take(ConfigTransitionItem::keyDefaultState);
        if (!valueTransitionItemDefaultState.isDouble()) {
            return false;
        }
        configTransitionItem.defaultstate = valueTransitionItemDefaultState.toInt();

        // Parse template
        QJsonValue valueTransitionItemTemplates = objTransitionItem.take(ConfigTransitionItem::keyTemplates);
        if (!valueTransitionItemTemplates.isArray()) {
            return false;
        }
        QJsonArray arrayTransitionItemTemplates = valueTransitionItemTemplates.toArray();
        for (int j = 0; j < arrayTransitionItemTemplates.size(); ++j) {
            // Parse templates item
            QJsonValue valueTransitionItemTemplatesItem = arrayTransitionItemTemplates.at(j);
            if (!valueTransitionItemTemplatesItem.isObject()) {
                return false;
            }
            QJsonObject objTransitionItemTemplatesItem = valueTransitionItemTemplatesItem.toObject();
            if (!objTransitionItemTemplatesItem.contains(ConfigTransitionItem::keyTemplatesPoint) ||
                !objTransitionItemTemplatesItem.contains(ConfigTransitionItem::keyTemplatesState)) {
                return false;
            }

            // Parse templates item point and state
            QJsonValue valueTransitionItemTemplatesItemPoint = objTransitionItemTemplatesItem.take(ConfigTransitionItem::keyTemplatesPoint);
            if (!valueTransitionItemTemplatesItemPoint.isDouble()) {
                return false;
            }
            QJsonValue valueTransitionItemTemplatesItemState = objTransitionItemTemplatesItem.take(ConfigTransitionItem::keyTemplatesState);
            if (!valueTransitionItemTemplatesItemState.isDouble()) {
                return false;
            }
            configTransitionItem.templates[valueTransitionItemTemplatesItemPoint.toInt()] = valueTransitionItemTemplatesItemState.toInt();
        }

        // Insert into ConfigTransition
        GYSCADAConfig::getInstance()->transition.transitions[key] = configTransitionItem;
    }

    return true;
}

bool GYSCADAConfig::parseMySQL(QJsonObject &objRoot)
{
    // Parse Gyscada
    if (!objRoot.contains(ConfigMySQL::key)) {
        return false;
    }
    QJsonValue valueMySQL = objRoot.take(ConfigMySQL::key);
    if (!valueMySQL.isObject()) {
        return false;
    }
    QJsonObject objMySQL = valueMySQL.toObject();
    if (!objMySQL.contains(ConfigMySQL::keyIp) ||
        !objMySQL.contains(ConfigMySQL::keyPort) ||
        !objMySQL.contains(ConfigMySQL::keyDB) ||
        !objMySQL.contains(ConfigMySQL::keyUser) ||
        !objMySQL.contains(ConfigMySQL::keyPassword) ||
        !objMySQL.contains(ConfigMySQL::keyUserTable) ||
        !objMySQL.contains(ConfigMySQL::keyStafferTable) ||
        !objMySQL.contains(ConfigMySQL::keyLogTable)) {
        return false;
    }

    // Parse ip
    QJsonValue valueMySQLIp = objMySQL.take(ConfigMySQL::keyIp);
    if (!valueMySQLIp.isString()) {
        return false;
    }
    GYSCADAConfig::getInstance()->mysql.ip = valueMySQLIp.toString();

    // Parse port
    QJsonValue valueMySQLPort = objMySQL.take(ConfigMySQL::keyPort);
    if (!valueMySQLPort.isDouble()) {
        return false;
    }
    GYSCADAConfig::getInstance()->mysql.port = valueMySQLPort.toInt();

    // Parse db
    QJsonValue valueMySQLDB = objMySQL.take(ConfigMySQL::keyDB);
    if (!valueMySQLDB.isString()) {
        return false;
    }
    GYSCADAConfig::getInstance()->mysql.db = valueMySQLDB.toString();

    // Parse user
    QJsonValue valueMySQLUser = objMySQL.take(ConfigMySQL::keyUser);
    if (!valueMySQLUser.isString()) {
        return false;
    }
    GYSCADAConfig::getInstance()->mysql.user = valueMySQLUser.toString();

    // Parse password
    QJsonValue valueMySQLPassword = objMySQL.take(ConfigMySQL::keyPassword);
    if (!valueMySQLPassword.isString()) {
        return false;
    }
    GYSCADAConfig::getInstance()->mysql.password = valueMySQLPassword.toString();

    // Parse user table
    QJsonValue valueMySQLUserTable = objMySQL.take(ConfigMySQL::keyUserTable);
    if (!valueMySQLUserTable.isString()) {
        return false;
    }
    GYSCADAConfig::getInstance()->mysql.user_table = valueMySQLUserTable.toString();

    // Parse staffer table
    QJsonValue valueMySQLStafferTable = objMySQL.take(ConfigMySQL::keyStafferTable);
    if (!valueMySQLStafferTable.isString()) {
        return false;
    }
    GYSCADAConfig::getInstance()->mysql.staffer_table = valueMySQLStafferTable.toString();

    // Parse log table
    QJsonValue valueMySQLLogTable = objMySQL.take(ConfigMySQL::keyLogTable);
    if (!valueMySQLLogTable.isString()) {
        return false;
    }
    GYSCADAConfig::getInstance()->mysql.log_table = valueMySQLLogTable.toString();

    return true;
}

bool GYSCADAConfig::parseGyscada(QJsonObject &objRoot)
{
    // Parse Gyscada
    if (!objRoot.contains(ConfigGyscada::keyGyscada)) {
        return false;
    }
    QJsonValue valueGyscada = objRoot.take(ConfigGyscada::keyGyscada);
    if (!valueGyscada.isObject()) {
        return false;
    }
    QJsonObject objGyscada = valueGyscada.toObject();
    if (!objGyscada.contains(ConfigGyscada::keyIp) ||
        !objGyscada.contains(ConfigGyscada::keyPort) ||
        !objGyscada.contains(ConfigGyscada::keyT0) ||
        !objGyscada.contains(ConfigGyscada::keyT1) ||
        !objGyscada.contains(ConfigGyscada::keyT2)) {
        return false;
    }

    // Parse ip
    QJsonValue valueGyscadaIp = objGyscada.take(ConfigGyscada::keyIp);
    if (!valueGyscadaIp.isString()) {
        return false;
    }
    GYSCADAConfig::getInstance()->gyscada.ip = valueGyscadaIp.toString();

    // Parse port
    QJsonValue valueGyscadaPort = objGyscada.take(ConfigGyscada::keyPort);
    if (!valueGyscadaPort.isDouble()) {
        return false;
    }
    GYSCADAConfig::getInstance()->gyscada.port = valueGyscadaPort.toInt();

    // Parse t0
    QJsonValue valueGyscadaT0 = objGyscada.take(ConfigGyscada::keyT0);
    if (!valueGyscadaT0.isDouble()) {
        return false;
    }
    GYSCADAConfig::getInstance()->gyscada.t0 = valueGyscadaT0.toInt();

    // Parse t1
    QJsonValue valueGyscadaT1 = objGyscada.take(ConfigGyscada::keyT1);
    if (!valueGyscadaT1.isDouble()) {
        return false;
    }
    GYSCADAConfig::getInstance()->gyscada.t1 = valueGyscadaT1.toInt();

    // Parse t2
    QJsonValue valueGyscadaT2 = objGyscada.take(ConfigGyscada::keyT2);
    if (!valueGyscadaT2.isDouble()) {
        return false;
    }
    GYSCADAConfig::getInstance()->gyscada.t2 = valueGyscadaT2.toInt();

    // Parse timer
    QJsonValue valueGyscadaTimer = objGyscada.take(ConfigGyscada::keyTimer);
    if (!valueGyscadaTimer.isDouble()) {
        return false;
    }
    GYSCADAConfig::getInstance()->gyscada.timer = valueGyscadaTimer.toInt();

    return true;
}

bool GYSCADAConfig::parsePoint(QJsonObject &objRoot)
{
    // Parse Point category
    if (!objRoot.contains(ConfigPoint::key)) {
        return false;
    }
    QJsonValue valuePoint = objRoot.take(ConfigPoint::key);
    if (!valuePoint.isArray()) {
        return false;
    }

    QJsonArray arrayPoint = valuePoint.toArray();
    for (int i = 0; i < arrayPoint.size(); ++i) {
        int key = 0;
        ConfigPointItem configPointItem;

        // Parse Point item
        QJsonValue valuePointItem = arrayPoint.at(i);
        if (!valuePointItem.isObject()) {
            return false;
        }
        QJsonObject objPointItem = valuePointItem.toObject();
        if (!objPointItem.contains(ConfigPointItem::keyId) ||
            !objPointItem.contains(ConfigPointItem::keyTitle) ||
            !objPointItem.contains(ConfigPointItem::keyUnit)) {
            return false;
        }

        // Parse id
        QJsonValue valuePointItemId = objPointItem.take(ConfigPointItem::keyId);
        if (!valuePointItemId.isDouble()) {
            return false;
        }
        key = configPointItem.id = valuePointItemId.toInt();

        // Parse title
        QJsonValue valuePointItemTitle = objPointItem.take(ConfigPointItem::keyTitle);
        if (!valuePointItemTitle.isString()) {
            return false;
        }
        configPointItem.title = valuePointItemTitle.toString();

        // Parse unit
        QJsonValue valuePointItemUnit = objPointItem.take(ConfigPointItem::keyUnit);
        if (!valuePointItemUnit.isString()) {
            return false;
        }
        configPointItem.unit = valuePointItemUnit.toString();

        // Insert into ConfigStation
        GYSCADAConfig::getInstance()->point.points[key] = configPointItem;
    }

    return true;
}

bool GYSCADAConfig::parseStation(QJsonObject &objRoot)
{
    // Parse Station category
    if (!objRoot.contains(ConfigStation::key)) {
        return false;
    }
    QJsonValue valueStation = objRoot.take(ConfigStation::key);
    if (!valueStation.isArray()) {
        return false;
    }

    QJsonArray arrayStation = valueStation.toArray();
    for (int i = 0; i < arrayStation.size(); ++i) {
        int key = 0;
        ConfigStationItem configStationItem;

        // Parse Station item
        QJsonValue valueStationItem = arrayStation.at(i);
        if (!valueStationItem.isObject()) {
            return false;
        }
        QJsonObject objStationItem = valueStationItem.toObject();
        if (!objStationItem.contains(ConfigStationItem::keyId) ||
            !objStationItem.contains(ConfigStationItem::keyName) ||
            !objStationItem.contains(ConfigStationItem::keyLines) ||
            !objStationItem.contains(ConfigStationItem::keyControls)) {
            return false;
        }

        // Parse id
        QJsonValue valueStationItemId = objStationItem.take(ConfigStationItem::keyId);
        if (!valueStationItemId.isDouble()) {
            return false;
        }
        key = configStationItem.id = valueStationItemId.toInt();

        // Parse name
        QJsonValue valueStationItemName = objStationItem.take(ConfigStationItem::keyName);
        if (!valueStationItemName.isString()) {
            return false;
        }
        configStationItem.name = valueStationItemName.toString();

        // Parse lines
        QJsonValue valueStationItemLines = objStationItem.take(ConfigStationItem::keyLines);
        if (!valueStationItemLines.isArray()) {
            return false;
        }
        QJsonArray arrayStationItemLines = valueStationItemLines.toArray();
        for (int j = 0; j < arrayStationItemLines.size(); ++j) {
            configStationItem.lines.append(arrayStationItemLines.at(j).toInt());
        }

        // Parse controls
        QJsonValue valueStationItemControls = objStationItem.take(ConfigStationItem::keyControls);
        if (!valueStationItemControls.isArray()) {
            return false;
        }
        QJsonArray arrayStationItemControls = valueStationItemControls.toArray();
        for (int j = 0; j < arrayStationItemControls.size(); ++j) {
            // Parse control item
            QJsonValue valueStationItemControlsItem = arrayStationItemControls.at(j);
            if (!valueStationItemControlsItem.isObject()) {
                return false;
            }
            QJsonObject objStationItemControlsItem = valueStationItemControlsItem.toObject();
            if (!objStationItemControlsItem.contains(ControlInfo::keyControlsId) ||
                !objStationItemControlsItem.contains(ControlInfo::keyControlsValue) ||
                !objStationItemControlsItem.contains(ControlInfo::keyControlsSupported) ||
                !objStationItemControlsItem.contains(ControlInfo::keyControlsAddr)) {
                return false;
            }

            // Parse controls item id value and addr
            ControlInfo controlInfo;
            QJsonValue valueStationItemControlsItemId = objStationItemControlsItem.take(ControlInfo::keyControlsId);
            if (!valueStationItemControlsItemId.isDouble()) {
                return false;
            }
            QJsonValue valueStationItemControlsItemSupported = objStationItemControlsItem.take(ControlInfo::keyControlsSupported);
            if (!valueStationItemControlsItemSupported.isDouble()) {
                return false;
            }
            QJsonValue valueStationItemControlsItemValue = objStationItemControlsItem.take(ControlInfo::keyControlsValue);
            if (!valueStationItemControlsItemValue.isDouble()) {
                return false;
            }
            QJsonValue valueStationItemControlsItemAddr = objStationItemControlsItem.take(ControlInfo::keyControlsAddr);
            if (!valueStationItemControlsItemAddr.isDouble()) {
                return false;
            }
            controlInfo.id = valueStationItemControlsItemId.toInt();
            controlInfo.supported = valueStationItemControlsItemSupported.toInt();
            controlInfo.value = valueStationItemControlsItemValue.toInt();
            controlInfo.addr = valueStationItemControlsItemAddr.toInt();
            configStationItem.controls[controlInfo.id] = controlInfo;
        }

        // Insert into ConfigStation
        GYSCADAConfig::getInstance()->station.stations[key] = configStationItem;
    }

    return true;
}

bool GYSCADAConfig::parseLine(QJsonObject &objRoot)
{
    // Parse Line category
    if (!objRoot.contains(ConfigLine::key)) {
        return false;
    }
    QJsonValue valueLine = objRoot.take(ConfigLine::key);
    if (!valueLine.isArray()) {
        return false;
    }
    QJsonArray arrayLine = valueLine.toArray();
    for (int i = 0; i < arrayLine.size(); ++i) {
        int key = 0;
        ConfigLineItem configLineItem;

        // Parse Line item
        QJsonValue valueLineItem = arrayLine.at(i);
        if (!valueLineItem.isObject()) {
            return false;
        }
        QJsonObject objLineItem = valueLineItem.toObject();
        if (!objLineItem.contains(ConfigLineItem::keyId) ||
            !objLineItem.contains(ConfigLineItem::keyName) ||
            !objLineItem.contains(ConfigLineItem::keyTurbines)) {
            return false;
        }

        // Parse id
        QJsonValue valueLineItemId = objLineItem.take(ConfigLineItem::keyId);
        if (!valueLineItemId.isDouble()) {
            return false;
        }
        key = configLineItem.id = valueLineItemId.toInt();

        // Parse name
        QJsonValue valueLineItemName = objLineItem.take(ConfigLineItem::keyName);
        if (!valueLineItemName.isString()) {
            return false;
        }
        configLineItem.name = valueLineItemName.toString();

        // Parse turbines
        QJsonValue valueLineItemTurbines = objLineItem.take(ConfigLineItem::keyTurbines);
        if (!valueLineItemTurbines.isArray()) {
            return false;
        }
        QJsonArray arrayLineItemTurbines = valueLineItemTurbines.toArray();
        for (int j = 0; j < arrayLineItemTurbines.size(); ++j) {
            configLineItem.turbines.append(arrayLineItemTurbines.at(j).toInt());
        }

        // Parse controls
        QJsonValue valueLineItemControls = objLineItem.take(ConfigLineItem::keyControls);
        if (!valueLineItemControls.isArray()) {
            return false;
        }
        QJsonArray arrayLineItemControls = valueLineItemControls.toArray();
        for (int j = 0; j < arrayLineItemControls.size(); ++j) {
            // Parse control item
            QJsonValue valueLineItemControlsItem = arrayLineItemControls.at(j);
            if (!valueLineItemControlsItem.isObject()) {
                return false;
            }
            QJsonObject objLineItemControlsItem = valueLineItemControlsItem.toObject();
            if (!objLineItemControlsItem.contains(ControlInfo::keyControlsId) ||
                !objLineItemControlsItem.contains(ControlInfo::keyControlsValue) ||
                !objLineItemControlsItem.contains(ControlInfo::keyControlsSupported) ||
                !objLineItemControlsItem.contains(ControlInfo::keyControlsAddr)) {
                return false;
            }

            // Parse controls item id value and addr
            ControlInfo controlInfo;
            QJsonValue valueLineItemControlsItemId = objLineItemControlsItem.take(ControlInfo::keyControlsId);
            if (!valueLineItemControlsItemId.isDouble()) {
                return false;
            }
            QJsonValue valueLineItemControlsItemSupported = objLineItemControlsItem.take(ControlInfo::keyControlsSupported);
            if (!valueLineItemControlsItemSupported.isDouble()) {
                return false;
            }
            QJsonValue valueLineItemControlsItemValue = objLineItemControlsItem.take(ControlInfo::keyControlsValue);
            if (!valueLineItemControlsItemValue.isDouble()) {
                return false;
            }
            QJsonValue valueLineItemControlsItemAddr = objLineItemControlsItem.take(ControlInfo::keyControlsAddr);
            if (!valueLineItemControlsItemAddr.isDouble()) {
                return false;
            }
            controlInfo.id = valueLineItemControlsItemId.toInt();
            controlInfo.supported = valueLineItemControlsItemSupported.toInt();
            controlInfo.value = valueLineItemControlsItemValue.toInt();
            controlInfo.addr = valueLineItemControlsItemAddr.toInt();
            configLineItem.controls[controlInfo.id] = controlInfo;
        }

        // Insert into ConfigLine
        GYSCADAConfig::getInstance()->line.lines[key] = configLineItem;
    }

    return true;
}

bool GYSCADAConfig::parseTurbine(QJsonObject &objRoot)
{
    // Parse Turbine category
    if (!objRoot.contains(ConfigTurbine::key)) {
        return false;
    }
    QJsonValue valueTurbine = objRoot.take(ConfigTurbine::key);
    if (!valueTurbine.isArray()) {
        return false;
    }
    QJsonArray arrayTurbine = valueTurbine.toArray();
    for (int i = 0; i < arrayTurbine.size(); ++i) {
        int key = 0;
        ConfigTurbineItem configTurbineItem;

        // Parse Turbine item
        QJsonValue valueTurbineItem = arrayTurbine.at(i);
        if (!valueTurbineItem.isObject()) {
            return false;
        }
        QJsonObject objTurbineItem = valueTurbineItem.toObject();
        if (!objTurbineItem.contains(ConfigTurbineItem::keyId) ||
            !objTurbineItem.contains(ConfigTurbineItem::keyName) ||
            !objTurbineItem.contains(ConfigTurbineItem::keyControls) ||
            !objTurbineItem.contains(ConfigTurbineItem::keyStatePoint) ||
            !objTurbineItem.contains(ConfigTurbineItem::keyStateTransition) ||
            !objTurbineItem.contains(ConfigTurbineItem::keyPoints)) {
            return false;
        }

        // Parse id
        QJsonValue valueTurbineItemId = objTurbineItem.take(ConfigTurbineItem::keyId);
        if (!valueTurbineItemId.isDouble()) {
            return false;
        }
        key = configTurbineItem.id = valueTurbineItemId.toInt();

        // Parse name
        QJsonValue valueTurbineItemName = objTurbineItem.take(ConfigTurbineItem::keyName);
        if (!valueTurbineItemName.isString()) {
            return false;
        }
        configTurbineItem.name = valueTurbineItemName.toString();

        // Parse state point
        QJsonValue valueTurbineItemStatePoint = objTurbineItem.take(ConfigTurbineItem::keyStatePoint);
        if (!valueTurbineItemStatePoint.isDouble()) {
            return false;
        }
        configTurbineItem.statepoint = valueTurbineItemStatePoint.toInt();

        // Parse state transition
        QJsonValue valueTurbineItemStateTransition = objTurbineItem.take(ConfigTurbineItem::keyStateTransition);
        if (!valueTurbineItemStateTransition.isDouble()) {
            return false;
        }
        configTurbineItem.statetransition = valueTurbineItemStateTransition.toInt();

        // Parse controls
        QJsonValue valueTurbineItemControls = objTurbineItem.take(ConfigTurbineItem::keyControls);
        if (!valueTurbineItemControls.isArray()) {
            return false;
        }
        QJsonArray arrayTurbineItemControls = valueTurbineItemControls.toArray();
        for (int j = 0; j < arrayTurbineItemControls.size(); ++j) {
            // Parse control item
            QJsonValue valueTurbineItemControlsItem = arrayTurbineItemControls.at(j);
            if (!valueTurbineItemControlsItem.isObject()) {
                return false;
            }
            QJsonObject objTurbineItemControlsItem = valueTurbineItemControlsItem.toObject();
            if (!objTurbineItemControlsItem.contains(ControlInfo::keyControlsId) ||
                !objTurbineItemControlsItem.contains(ControlInfo::keyControlsValue) ||
                !objTurbineItemControlsItem.contains(ControlInfo::keyControlsSupported) ||
                !objTurbineItemControlsItem.contains(ControlInfo::keyControlsAddr)) {
                return false;
            }

            // Parse controls item id value and addr
            ControlInfo controlInfo;
            QJsonValue valueTurbineItemControlsItemId = objTurbineItemControlsItem.take(ControlInfo::keyControlsId);
            if (!valueTurbineItemControlsItemId.isDouble()) {
                return false;
            }
            QJsonValue valueTurbineItemControlsItemSupported = objTurbineItemControlsItem.take(ControlInfo::keyControlsSupported);
            if (!valueTurbineItemControlsItemSupported.isDouble()) {
                return false;
            }
            QJsonValue valueTurbineItemControlsItemValue = objTurbineItemControlsItem.take(ControlInfo::keyControlsValue);
            if (!valueTurbineItemControlsItemValue.isDouble()) {
                return false;
            }
            QJsonValue valueTurbineItemControlsItemAddr = objTurbineItemControlsItem.take(ControlInfo::keyControlsAddr);
            if (!valueTurbineItemControlsItemAddr.isDouble()) {
                return false;
            }
            controlInfo.id = valueTurbineItemControlsItemId.toInt();
            controlInfo.supported = valueTurbineItemControlsItemSupported.toInt();
            controlInfo.value = valueTurbineItemControlsItemValue.toInt();
            controlInfo.addr = valueTurbineItemControlsItemAddr.toInt();
            configTurbineItem.controls[controlInfo.id] = controlInfo;
        }

        // Parse points
        QJsonValue valueTurbineItemPoints = objTurbineItem.take(ConfigTurbineItem::keyPoints);
        if (!valueTurbineItemPoints.isArray()) {
            return false;
        }
        QJsonArray arrayTurbineItemPoints = valueTurbineItemPoints.toArray();
        for (int j = 0; j < arrayTurbineItemPoints.size(); ++j) {
            // Parse point item
            QJsonValue valueTurbineItemPointsItem = arrayTurbineItemPoints.at(j);
            if (!valueTurbineItemPointsItem.isObject()) {
                return false;
            }
            QJsonObject objTurbineItemPointsItem = valueTurbineItemPointsItem.toObject();
            if (!objTurbineItemPointsItem.contains(ConfigTurbineItem::keyPointsId) ||
                !objTurbineItemPointsItem.contains(ConfigTurbineItem::keyPointsAddr)) {
                return false;
            }

            // Parse points item id and addr
            QJsonValue valueTurbineItemPointsItemId = objTurbineItemPointsItem.take(ConfigTurbineItem::keyPointsId);
            if (!valueTurbineItemPointsItemId.isDouble()) {
                return false;
            }
            QJsonValue valueTurbineItemPointsItemAddr = objTurbineItemPointsItem.take(ConfigTurbineItem::keyPointsAddr);
            if (!valueTurbineItemPointsItemAddr.isString()) {
                return false;
            }
            configTurbineItem.points[valueTurbineItemPointsItemId.toInt()] = valueTurbineItemPointsItemAddr.toString();
        }

        // Insert into ConfigTurbine
        GYSCADAConfig::getInstance()->turbine.turbines[key] = configTurbineItem;
    }

    return true;
}

bool GYSCADAConfig::parseState(QJsonObject &objRoot)
{
    // Parse state category
    if (!objRoot.contains(ConfigState::key)) {
        return false;
    }
    QJsonValue valueState = objRoot.take(ConfigState::key);
    if (!valueState.isArray()) {
        return false;
    }
    QJsonArray arrayState = valueState.toArray();
    for (int i = 0; i < arrayState.size(); ++i) {
        int key = 0;
        ConfigStateItem configStateItem;

        // Parse state item
        QJsonValue valueStateItem = arrayState.at(i);
        if (!valueStateItem.isObject()) {
            return false;
        }
        QJsonObject objStateItem = valueStateItem.toObject();
        if (!objStateItem.contains(ConfigStateItem::keyId) ||
            !objStateItem.contains(ConfigStateItem::keyTitle) ||
            !objStateItem.contains(ConfigStateItem::keyDescription) ||
            !objStateItem.contains(ConfigStateItem::keyColor) ||
            !objStateItem.contains(ConfigStateItem::keyIcon)) {
            return false;
        }

        // Parse id
        QJsonValue valueStateItemId = objStateItem.take(ConfigStateItem::keyId);
        if (!valueStateItemId.isDouble()) {
            return false;
        }
        key = configStateItem.id = valueStateItemId.toInt();

        // Parse title
        QJsonValue valueStateItemTitle = objStateItem.take(ConfigStateItem::keyTitle);
        if (!valueStateItemTitle.isString()) {
            return false;
        }
        configStateItem.title = valueStateItemTitle.toString();

        // Parse description
        QJsonValue valueStateItemDescription = objStateItem.take(ConfigStateItem::keyDescription);
        if (!valueStateItemDescription.isString()) {
            return false;
        }
        configStateItem.description = valueStateItemDescription.toString();

        // Parse color
        QJsonValue valueStateItemColor = objStateItem.take(ConfigStateItem::keyColor);
        if (!valueStateItemColor.isArray()) {
            return false;
        }
        QJsonArray arrayStateItemColor = valueStateItemColor.toArray();
        if (arrayStateItemColor.size() != 3) {
            // 3 is count of color R,G,B.
            return false;
        }
        configStateItem.color.setRgb(
            arrayStateItemColor.at(0).toInt(),
            arrayStateItemColor.at(1).toInt(),
            arrayStateItemColor.at(2).toInt());

        // Parse icon
        QJsonValue valueStateItemIcon = objStateItem.take(ConfigStateItem::keyIcon);
        if (!valueStateItemIcon.isString()) {
            return false;
        }
        configStateItem.icon.addFile(valueStateItemIcon.toString());

        // Insert into ConfigState
        GYSCADAConfig::getInstance()->state.states[key] = configStateItem;
    }

    return true;
}

bool GYSCADAConfig::parseControl(QJsonObject& objRoot)
{
    // Parse control category
    if (!objRoot.contains(ConfigControl::key)) {
        return false;
    }
    QJsonValue valueControl = objRoot.take(ConfigControl::key);
    if (!valueControl.isArray()) {
        return false;
    }
    QJsonArray arrayControl = valueControl.toArray();
    for (int i = 0; i < arrayControl.size(); ++i) {
        int key = 0;
        ConfigControlItem configControlItem;

        // Parse control item
        QJsonValue valueControlItem = arrayControl.at(i);
        if (!valueControlItem.isObject()) {
            return false;
        }
        QJsonObject objControlItem = valueControlItem.toObject();
        if (!objControlItem.contains(ConfigControlItem::keyId) ||
            !objControlItem.contains(ConfigControlItem::keyTitle) ||
            !objControlItem.contains(ConfigControlItem::keyDescription)) {
            return false;
        }

        // Parse id
        QJsonValue valueControlItemId = objControlItem.take(ConfigControlItem::keyId);
        if (!valueControlItemId.isDouble()) {
            return false;
        }
        key = configControlItem.id = valueControlItemId.toInt();

        // Parse title
        QJsonValue valueControlItemTitle = objControlItem.take(ConfigControlItem::keyTitle);
        if (!valueControlItemTitle.isString()) {
            return false;
        }
        configControlItem.title = valueControlItemTitle.toString();

        // Parse description
        QJsonValue valueControlItemDescription = objControlItem.take(ConfigControlItem::keyDescription);
        if (!valueControlItemDescription.isString()) {
            return false;
        }
        configControlItem.description = valueControlItemDescription.toString();

        // Parse shortcut
        QJsonValue valueControlItemShortCut = objControlItem.take(ConfigControlItem::keyShortCut);
        if (!valueControlItemShortCut.isString()) {
            return false;
        }
        configControlItem.shortcut = valueControlItemShortCut.toString();

        // Parse Icon
        QJsonValue valueControlItemIcon = objControlItem.take(ConfigControlItem::keyIcon);
        if (!valueControlItemIcon.isString()) {
            return false;
        }
        configControlItem.icon = valueControlItemIcon.toString();

        // Parse type
        QJsonValue valueControlItemType = objControlItem.take(ConfigControlItem::keyType);
        if (!valueControlItemType.isDouble()) {
            return false;
        }
        configControlItem.type = valueControlItemType.toInt();

        // Parse getDouble item
        if (configControlItem.type == ControlInfo::ControlTypeYT) {
            // parse dlgtitle
            QJsonValue valueControlItemDlgTitle = objControlItem.take(ConfigControlItem::keyDlgTitle);
            if (!valueControlItemDlgTitle.isString()) {
                return false;
            }
            configControlItem.dlgtitle = valueControlItemDlgTitle.toString();

            // parse defaultvalue
            QJsonValue valueControlItemDefaultValue = objControlItem.take(ConfigControlItem::keyDefaultValue);
            if (!valueControlItemDefaultValue.isDouble()) {
                return false;
            }
            configControlItem.defaultvalue = valueControlItemDefaultValue.toDouble();

            // parse minvalue
            QJsonValue valueControlItemMinValue = objControlItem.take(ConfigControlItem::keyMinValue);
            if (!valueControlItemMinValue.isDouble()) {
                return false;
            }
            configControlItem.minvalue = valueControlItemMinValue.toDouble();

            // parse maxvalue
            QJsonValue valueControlItemMaxValue = objControlItem.take(ConfigControlItem::keyMaxValue);
            if (!valueControlItemMaxValue.isDouble()) {
                return false;
            }
            configControlItem.maxvalue = valueControlItemMaxValue.toDouble();

            // Parse decimals
            QJsonValue valueControlItemDecimals = objControlItem.take(ConfigControlItem::keyDecimals);
            if (!valueControlItemDecimals.isDouble()) {
                return false;
            }
            configControlItem.decimals = valueControlItemDecimals.toInt();
        }

        // Insert into ConfigControl
        GYSCADAConfig::getInstance()->control.controls[key] = configControlItem;
    }

    return true;
}

// Get Control config item
QList<int> GYSCADAConfig::getControlIds()
{
    return GYSCADAConfig::getInstance()->control.controls.keys();
}

int GYSCADAConfig::getControlType(int id)
{
    if (GYSCADAConfig::getInstance()->control.controls.contains(id)) {
        return GYSCADAConfig::getInstance()->control.controls[id].type;
    } else {
        return -1;
    }
}

QIcon GYSCADAConfig::getControlIcon(int id)
{
    if (GYSCADAConfig::getInstance()->control.controls.contains(id)) {
        return QIcon(GYSCADAConfig::getInstance()->control.controls[id].icon);
    } else {
        return QIcon();
    }
}

QString GYSCADAConfig::getControlTitle(int id)
{
    if (GYSCADAConfig::getInstance()->control.controls.contains(id)) {
        return GYSCADAConfig::getInstance()->control.controls[id].title;
    } else {
        return QString();
    }
}

QString GYSCADAConfig::getControlDlgTitle(int id)
{
    if (GYSCADAConfig::getInstance()->control.controls.contains(id)) {
        return GYSCADAConfig::getInstance()->control.controls[id].dlgtitle;
    } else {
        return QString();
    }
}

QString GYSCADAConfig::getControlShortCut(int id)
{
    if (GYSCADAConfig::getInstance()->control.controls.contains(id)) {
        return GYSCADAConfig::getInstance()->control.controls[id].shortcut;
    } else {
        return QString();
    }
}

QString GYSCADAConfig::getControlDescription(int id)
{
    if (GYSCADAConfig::getInstance()->control.controls.contains(id)) {
        return GYSCADAConfig::getInstance()->control.controls[id].description;
    } else {
        return QString();
    }
}

double GYSCADAConfig::getControlDefaultValue(int id)
{
    if (GYSCADAConfig::getInstance()->control.controls.contains(id)) {
        return GYSCADAConfig::getInstance()->control.controls[id].defaultvalue;
    } else {
        return 0;
    }
}

double GYSCADAConfig::getControlMaxValue(int id)
{
    if (GYSCADAConfig::getInstance()->control.controls.contains(id)) {
        return GYSCADAConfig::getInstance()->control.controls[id].maxvalue;
    } else {
        return 0;
    }
}

double GYSCADAConfig::getControlMinValue(int id)
{
    if (GYSCADAConfig::getInstance()->control.controls.contains(id)) {
        return GYSCADAConfig::getInstance()->control.controls[id].minvalue;
    } else {
        return 0;
    }
}

int GYSCADAConfig::getControlDecimals(int id)
{
    if (GYSCADAConfig::getInstance()->control.controls.contains(id)) {
        return GYSCADAConfig::getInstance()->control.controls[id].decimals;
    } else {
        return 0;
    }
}

// Get Station config item;
QList<int> GYSCADAConfig::getStationIds()
{
    return GYSCADAConfig::getInstance()->station.stations.keys();
}

QString GYSCADAConfig::getStationName(int id)
{
    if (GYSCADAConfig::getInstance()->station.stations.contains(id)) {
        return GYSCADAConfig::getInstance()->station.stations[id].name;
    } else {
        return QString();
    }
}

QList<int> GYSCADAConfig::getStationLines(int id)
{
    if (GYSCADAConfig::getInstance()->station.stations.contains(id)) {
        return GYSCADAConfig::getInstance()->station.stations[id].lines;
    } else {
        return QList<int>();
    }
}

QMap<int, ControlInfo> GYSCADAConfig::getStationControls(int id)
{
    if (GYSCADAConfig::getInstance()->station.stations.contains(id)) {
        return GYSCADAConfig::getInstance()->station.stations[id].controls;
    } else {
        return QMap<int, ControlInfo>();
    }
}

// Get Line config items;
QList<int> GYSCADAConfig::getLineIds()
{
    return GYSCADAConfig::getInstance()->line.lines.keys();
}

QString GYSCADAConfig::getLineName(int id)
{
    if (GYSCADAConfig::getInstance()->line.lines.contains(id)) {
        return GYSCADAConfig::getInstance()->line.lines[id].name;
    } else {
        return QString();
    }
}

QList<int> GYSCADAConfig::getLineTurbines(int id)
{
    if (GYSCADAConfig::getInstance()->line.lines.contains(id)) {
        return GYSCADAConfig::getInstance()->line.lines[id].turbines;
    } else {
        return QList<int>();
    }
}

QMap<int, ControlInfo> GYSCADAConfig::getLineControls(int id)
{
    if (GYSCADAConfig::getInstance()->line.lines.contains(id)) {
        return GYSCADAConfig::getInstance()->line.lines[id].controls;
    } else {
        return QMap<int, ControlInfo>();
    }
}

// Get Turbine config items;
QList<int> GYSCADAConfig::getTurbineIds()
{
    return GYSCADAConfig::getInstance()->turbine.turbines.keys();
}

QString GYSCADAConfig::getTurbineName(int id)
{
    if (GYSCADAConfig::getInstance()->turbine.turbines.contains(id)) {
        return GYSCADAConfig::getInstance()->turbine.turbines[id].name;
    } else {
        return QString();
    }
}

QMap<int, ControlInfo> GYSCADAConfig::getTurbineControls(int id)
{
    if (GYSCADAConfig::getInstance()->turbine.turbines.contains(id)) {
        return GYSCADAConfig::getInstance()->turbine.turbines[id].controls;
    } else {
        return QMap<int, ControlInfo>();
    }
}

QMap<int, int> GYSCADAConfig::getTurbineTransition(int id)
{
    if (GYSCADAConfig::getInstance()->turbine.turbines.contains(id)) {
        return GYSCADAConfig::getInstance()->getTransitionTemplate(
                   GYSCADAConfig::getInstance()->turbine.turbines[id].statetransition);
    } else {
        return QMap<int, int>();
    }
}

int GYSCADAConfig::getTurbineDefaultState(int id)
{
    if (GYSCADAConfig::getInstance()->turbine.turbines.contains(id)) {
        return GYSCADAConfig::getInstance()->getTransitionDefaultState(
                   GYSCADAConfig::getInstance()->turbine.turbines[id].statetransition);
    } else {
        return -1;
    }
}

int GYSCADAConfig::getTurbineStatePoint(int id)
{
    if (GYSCADAConfig::getInstance()->turbine.turbines.contains(id)) {
        return GYSCADAConfig::getInstance()->turbine.turbines[id].statepoint;
    } else {
        return -1;
    }
}

QMap<int, QString> GYSCADAConfig::getTurbinePoints(int id)
{
    if (GYSCADAConfig::getInstance()->turbine.turbines.contains(id)) {
        return GYSCADAConfig::getInstance()->turbine.turbines[id].points;
    } else {
        return QMap<int, QString>();
    }
}

QMap<int, int> GYSCADAConfig::getTransitionTemplate(int id)
{
    if (GYSCADAConfig::getInstance()->transition.transitions.contains(id)) {
        return GYSCADAConfig::getInstance()->transition.transitions[id].templates;
    } else {
        return QMap<int, int>();
    }
}

int GYSCADAConfig::getTransitionDefaultState(int id)
{
    if (GYSCADAConfig::getInstance()->transition.transitions.contains(id)) {
        return GYSCADAConfig::getInstance()->transition.transitions[id].defaultstate;
    } else {
        return -1;
    }
}

QList<int> GYSCADAConfig::getPointIds()
{
    return GYSCADAConfig::getInstance()->point.points.keys();
}

QString GYSCADAConfig::getPointTitle(int id)
{
    if (GYSCADAConfig::getInstance()->point.points.contains(id)) {
        return GYSCADAConfig::getInstance()->point.points[id].title;
    } else {
        return QString();
    }
}

QString GYSCADAConfig::getPointUnit(int id)
{
    if (GYSCADAConfig::getInstance()->point.points.contains(id)) {
        return GYSCADAConfig::getInstance()->point.points[id].unit;
    } else {
        return QString();
    }
}

// Get State config item
QString GYSCADAConfig::getStateTitle(int id)
{
    if (GYSCADAConfig::getInstance()->state.states.contains(id)) {
        return GYSCADAConfig::getInstance()->state.states[id].title;
    } else {
        return QString();
    }
}

QColor GYSCADAConfig::getStateColor(int id)
{
    if (GYSCADAConfig::getInstance()->state.states.contains(id)) {
        return GYSCADAConfig::getInstance()->state.states[id].color;
    } else {
        return QColor();
    }
}

QIcon GYSCADAConfig::getStateIcon(int id)
{
    if (GYSCADAConfig::getInstance()->state.states.contains(id)) {
        return GYSCADAConfig::getInstance()->state.states[id].icon;
    } else {
        return QIcon();
    }
}

QString GYSCADAConfig::getGyscadaIp()
{
    return GYSCADAConfig::getInstance()->gyscada.ip;
}

int GYSCADAConfig::getGyscadaPort()
{
    return GYSCADAConfig::getInstance()->gyscada.port;
}

int GYSCADAConfig::getGyscadaT0()
{
    return GYSCADAConfig::getInstance()->gyscada.t0;
}

int GYSCADAConfig::getGyscadaT1()
{
    return GYSCADAConfig::getInstance()->gyscada.t1;
}

int GYSCADAConfig::getGyscadaT2()
{
    return GYSCADAConfig::getInstance()->gyscada.t2;
}

int GYSCADAConfig::getGyscadaTimer()
{
    return GYSCADAConfig::getInstance()->gyscada.timer;
}

QString GYSCADAConfig::getMySQLIp()
{
    return GYSCADAConfig::getInstance()->mysql.ip;
}

int GYSCADAConfig::getMySQLPort()
{
    return GYSCADAConfig::getInstance()->mysql.port;
}

QString GYSCADAConfig::getMySQLDB()
{
    return GYSCADAConfig::getInstance()->mysql.db;
}

QString GYSCADAConfig::getMySQLUser()
{
    return GYSCADAConfig::getInstance()->mysql.user;
}

QString GYSCADAConfig::getMySQLPassword()
{
    return GYSCADAConfig::getInstance()->mysql.password;
}

QString GYSCADAConfig::getMySQLUserTable()
{
    return GYSCADAConfig::getInstance()->mysql.user_table;
}

QString GYSCADAConfig::getMySQLLogTable()
{
    return GYSCADAConfig::getInstance()->mysql.log_table;
}

QString GYSCADAConfig::getMySQLStafferTable()
{
    return GYSCADAConfig::getInstance()->mysql.staffer_table;
}

QList<ConfigRouterItem>& GYSCADAConfig::getRouterList()
{
    return GYSCADAConfig::getInstance()->router.routers;
}
